Extract JSON tags and build a new Json

get value from json object in javascript
json formatter
json validator
json viewer
json beautifier
json parser
how to get key and value from json array object in javascript
generate jsonpath from json

Similar to this question I want to extract some pieces of a JSON and build a new one just with the info I need. I tried with this code:

wanted_milestones_ids =  [ch_epic['milestone_id'],ch_epic['stats']['num_points'] for ch_epic in ch_epics if ch_epic['milestone_id'] is not None and set(CLUBHOUSE_PROJECT_IDS).intersection(ch_epic['project_ids']) and ch_epic['id'] == 16919]

Filter is working good, and actually I can get the values. The problem is that I obtain ALL values or ONE value, I need some of them, that's why I tried with: "ch_epic['milestone_id'],ch_epic['stats']['num_points']" But there was an error.

How can I achieve this task? Below is the original JSON code and I want to extract: Id, milestone_id and num_points.

{
    "archived": false,
    "started": false,
    "entity_type": "epic",
    "labels": [
      {
        "entity_type": "label",
        "id": 1305552,
        "created_at": "2018-10-01T16:37:13Z",
        "updated_at": "2018-11-13T14:25:23Z",
        "name": "random name",
        "color": "#e885d1",
        "external_id": null,
        "archived": false
      }
    ],
    "external_tickets": [],
    "mention_ids": [],
    "member_mention_ids": [],
    "project_ids": [
      2022
    ],
    "completed_at_override": null,
    "started_at": null,
    "completed_at": null,
    "name": "Randome Name",
    "completed": false,
    "state": "to do",
    "milestone_id": 195343762,
    "requested_by_id": "Goofy",
    "epic_state_id": 50230002327020,
    "started_at_override": null,
    "updated_at": "2019-08-08T21:19:00Z",
    "group_mention_ids": [],
    "support_tickets": [],
    "follower_ids": [
      "Donald"
    ],
    "owner_ids": [],
    "external_id": null,
    "id": 16965,
    "position": 961,
    "deadline": null,
    "stats": {
      "num_points_done": 1,
      "average_cycle_time": 32223231706,
      "num_stories_unstarted": 2,
      "last_story_update": "2019-10-02T12:24:08Z",
      "num_points_started": 1,
      "num_points_unstarted": 3,
      "num_stories_started": 1,
      "num_stories_unestimated": 2,
      "average_lead_time": 3305323270,
      "num_points": 5,
      "num_stories_done": 2
    },
    "created_at": "2019-03-28T16:39:50Z"
  }

I am not sure about why you are overcomplicating this...

Maybe I missed something?

Here's a simple extraction:

import json

data = r'{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" }'

data = data.replace('\n', '')
json_data = json.loads(data)
id = json_data['labels'][0]['id']
milestone_id = json_data['milestone_id']
num_points = json_data['stats']['num_points']

print("id:\t\t{}".format(id))
print("milestone_id:\t{}".format(milestone_id))
print("num_points:\t{}".format(num_points))

output = {'id':id, 'milestone_id':milestone_id, 'num_points':num_points}
final_output = json.dumps(output)

print("\njson:\t\t" + final_output)

Output

id:             1305552
milestone_id:   195343762
num_points:     5

json:           {"num_points": 5, "id": 1305552, "milestone_id": 195343762}

Reshaping JSON with jq, The array operator: []; The pipe: |; Filter: select(); Create new JSON: [] and to use the command-line utility jq to filter and parse complex JSON files into flat CSV files. To make clear what has happened, check the “Compact Output” [.​tag, .count] | Create simple arrays with just the tag name and count  GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up C# JSON API project


You could try to use the JSON package.

If you have the JSON saved in a file called example.json in the same directory as your python script. You could use the following code to get the desired fields.

import json

with open("example.json", "r") as read_file:
    data = json.load(read_file)

output_file = json.dumps({'id':data['id'], 'milestone_id':data['milestone_id'], 'num_points':data['stats']['num_points']})

JSON JSONPath – REST API Tutorial, In the same line, JSONPath is a query language with features similar to XPath that lets you extract just the bits of a JSON document your application needs. To use JSONPath, you will need to Live Example : Learn to build JSONPath expressions in line editor mappingProvider( new GsonMappingProvider()) .​build();. Thanks for this, its exactly the code to solve the issue. The problem is I didnt include a real world example of the JSON. I have edited my Question. The JSON will contain multiple keys. Is there a way to adjust this to extract the values for a specific key, "N.data.sessionDates-7f1790d3-9175-43aa-962b-161ee3b8615f" in the example.


Adapt it to multiple values:

import json

data = r'[{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" },{ "archived": false, "started": false, "entity_type": "epic", "labels": [ { "entity_type": "label", "id": 1305552, "created_at": "2018-10-01T16:37:13Z", "updated_at": "2018-11-13T14:25:23Z", "name": "random name", "color": "#e885d1", "external_id": null, "archived": false } ], "external_tickets": [], "mention_ids": [], "member_mention_ids": [], "project_ids": [ 2022 ], "completed_at_override": null, "started_at": null, "completed_at": null, "name": "Randome Name", "completed": false, "state": "to do", "milestone_id": 195343762, "requested_by_id": "Goofy", "epic_state_id": 50230002327020, "started_at_override": null, "updated_at": "2019-08-08T21:19:00Z", "group_mention_ids": [], "support_tickets": [], "follower_ids": [ "Donald" ], "owner_ids": [], "external_id": null, "id": 16965, "position": 961, "deadline": null, "stats": { "num_points_done": 1, "average_cycle_time": 32223231706, "num_stories_unstarted": 2, "last_story_update": "2019-10-02T12:24:08Z", "num_points_started": 1, "num_points_unstarted": 3, "num_stories_started": 1, "num_stories_unestimated": 2, "average_lead_time": 3305323270, "num_points": 5, "num_stories_done": 2 }, "created_at": "2019-03-28T16:39:50Z" }]'

data = data.replace('\n', '')
json_data = json.loads(data)
output = []
for milestone in json_data:
    id = milestone['labels'][0]['id']
    milestone_id = milestone['milestone_id']
    num_points = milestone['stats']['num_points']

# print("id:\t\t{}".format(id))
# print("milestone_id:\t{}".format(milestone_id))
# print("num_points:\t{}".format(num_points))

    output.append({'id':id, 'milestone_id':milestone_id, 'num_points':num_points})
final_output = json.dumps(output)
print("\njson:\t\t" + final_output)

output:

json: [{"id": 1305552, "milestone_id": 195343762, "num_points": 5}, {"id": 1305552, "milestone_id": 195343762, "num_points": 5}]

Working with JSON, JSON is a text-based data format following JavaScript object syntax, which was environments feature the ability to read (parse) and generate JSON. To create a request, we need to create a new request object instance  I want to extract the values of the three "name" fields and store it in three variables. I tried cat json_file | jq '.["example.sub-example.name"]' to extract the value of the "name" field but that doesn't work. Can anyone tell me how to achieve this using jq (or some other method)?


JSON.parse(), HTML Tag Reference HTML Browser Support HTML Event Reference HTML Color Reference HTML Attribute Reference Parse the data with JSON.parse() , and the data becomes a JavaScript object. Make sure the text is written in JSON format, or else you will get a syntax error. var xmlhttp = new XMLHttpRequest(); Online tool for querying, extracting or selecting parts of a JSON document or testing a query using JSONPath, JSPath, Lodash, Underscore, JPath, XPath for JSON, JSON Pointer or just plain old JavaScript. Enter your JSON and your query and immediately see the extracted results in the browser.


JSON Introduction, JSON is text, and we can convert any JavaScript object into JSON, and send JSON This way we can work with the data as JavaScript objects, with no complicated parsing and translations. var myObj = {name: "John", age: 31, city: "New York"}; JavaScript has a built in function to convert a string, written in JSON format,  JSON data in SQL Server. 06/03/2020; 13 minutes to read +25; In this article. APPLIES TO: SQL Server 2016 and later Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse . JSON is a popular textual data format that's used for exchanging data in modern web and mobile applications.


The JSON_QUERY() function to extract objects from JSON Data, We use square brackets to represent an array in the JSON Data. In the Overview of built-in JSON support Example 1: Get the JSON object from a JSON string SELECT TOP 1 JSON_QUERY(Tags) AS Tags, and high availability and disaster recovery technologies, learning new things, new features​. JSON_EXTRACT or JSON_EXTRACT_SCALAR. JSON_EXTRACT(json_string_expr, json_path_string_literal), which returns JSON values as STRINGs. JSON_EXTRACT_SCALAR(json_string_expr, json_path_string_literal), which returns scalar JSON values as STRINGs. Description. Extracts JSON values or JSON scalar values as strings. json_string_expr: A JSON-formatted